package constant.milk.cashbook.sms;

import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.tip.TipInfoActivity;
import constant.milk.cashbook.tip.TipMap;
import constant.milk.cashbook.util.CalendarActivity;
import constant.milk.cashbook.util.MilkUtil;

public class SmsPastActivity extends SuperActivity
{
	public static final int DATE1_REQUEST_CODE = 0;
	public static final int DATE2_REQUEST_CODE = 1;
	
	private TextView date1TextView;
	private TextView date2TextView;
	
	private View date1View;
	private View date2View;
	
	private View todayView;
	private View oneWeekView;
	private View oneMonthView;
	private View threeMonthView;
	private View halfYearView;
	private View oneYearView;
	
	private Button selectButton;
	
	private int year1;
	private int month1;
	private int day1;
	
	private int year2;
	private int month2;
	private int day2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.sms_past_activity);
		
		setTitle("지난문자조회");
		
		setFindViewId();
		setOnClick();
		setData();
	}
	
	@Override
	protected void setFindViewId()
	{
		date1TextView = (TextView) findViewById(R.id.smsPastDate1TextView);
		date2TextView = (TextView) findViewById(R.id.smsPastDate2TextView);
		
		date1View = findViewById(R.id.smsPastDate1View);
		date2View = findViewById(R.id.smsPastDate2View);
		
		todayView = findViewById(R.id.smsPastTodayView);
		oneWeekView = findViewById(R.id.smsPastOneWeekView);
		oneMonthView = findViewById(R.id.smsPastOneMonthView);
		threeMonthView = findViewById(R.id.smsPastThreeMonthView);
		halfYearView = findViewById(R.id.smsPastHalfYearView);
		oneYearView = findViewById(R.id.smsPastOneYearView);
		
		selectButton = (Button) findViewById(R.id.selectButton);
	}

	@Override
	protected void setOnClick()
	{
		date1View.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, CalendarActivity.class);
				intent.putExtra(CalendarActivity.YEAR, year1);
				intent.putExtra(CalendarActivity.MONTN, month1);
				intent.putExtra(CalendarActivity.DAY, day1);
				startActivityForResult(intent, DATE1_REQUEST_CODE);
			}
		});
		
		date2View.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, CalendarActivity.class);
				intent.putExtra(CalendarActivity.YEAR, year2);
				intent.putExtra(CalendarActivity.MONTN, month2);
				intent.putExtra(CalendarActivity.DAY, day2);
				startActivityForResult(intent, DATE2_REQUEST_CODE);
			}
		});
		
		todayView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setToDate();
			}
		});
		
		oneWeekView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setOneWeekDate();
			}
		});
		
		oneMonthView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setOneMonthDate();
			}
		});
		
		threeMonthView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setThreeMonthDate();
			}
		});
		
		halfYearView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setHalfYearDate();
			}
		});
		
		oneYearView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setOneYearDate();
			}
		});
		
		selectButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, SmsPastListActvitiy.class);
				intent.putExtra(SmsPastListActvitiy.YEAR1_DATA, year1);
				intent.putExtra(SmsPastListActvitiy.MONTH1_DATA, month1);
				intent.putExtra(SmsPastListActvitiy.DAY1_DATA, day1);
				intent.putExtra(SmsPastListActvitiy.YEAR2_DATA, year2);
				intent.putExtra(SmsPastListActvitiy.MONTH2_DATA, month2);
				intent.putExtra(SmsPastListActvitiy.DAY2_DATA, day2);
				startActivity(intent);
			}
		});
	}

	@Override
	protected void setData()
	{
		setToDate();
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == DATE1_REQUEST_CODE)
		{
			Calendar cal = Calendar.getInstance();
			int tyear1 = data.getIntExtra(CalendarActivity.YEAR, cal.get(Calendar.YEAR));
			int tmonth1 = data.getIntExtra(CalendarActivity.MONTN, cal.get(Calendar.MONTH));
			int tday1 = data.getIntExtra(CalendarActivity.DAY, cal.get(Calendar.DAY_OF_MONTH));
			
			int date1 = Integer.parseInt(MilkUtil.getDateDB(tyear1, tmonth1, tday1));
			int date2 = Integer.parseInt(MilkUtil.getDateDB(year2, month2, day2));
			
			if (!setDateCheck(date1, date2))
			{
				return;
			}
			
			year1 = data.getIntExtra(CalendarActivity.YEAR, cal.get(Calendar.YEAR));
			month1 = data.getIntExtra(CalendarActivity.MONTN, cal.get(Calendar.MONTH));
			day1 = data.getIntExtra(CalendarActivity.DAY, cal.get(Calendar.DAY_OF_MONTH));
			
			setDate1();
		}
		else if (requestCode == DATE2_REQUEST_CODE)
		{
			Calendar cal = Calendar.getInstance();
			int tyear2 = data.getIntExtra(CalendarActivity.YEAR, cal.get(Calendar.YEAR));
			int tmonth2 = data.getIntExtra(CalendarActivity.MONTN, cal.get(Calendar.MONTH));
			int tday2 = data.getIntExtra(CalendarActivity.DAY, cal.get(Calendar.DAY_OF_MONTH));
			
			int date1 = Integer.parseInt(MilkUtil.getDateDB(year1, month1, day1));
			int date2 = Integer.parseInt(MilkUtil.getDateDB(tyear2, tmonth2, tday2));
			
			if (!setDateCheck(date1, date2))
			{
				return;
			}
			
			year2 = data.getIntExtra(CalendarActivity.YEAR, cal.get(Calendar.YEAR));
			month2 = data.getIntExtra(CalendarActivity.MONTN, cal.get(Calendar.MONTH));
			day2 = data.getIntExtra(CalendarActivity.DAY, cal.get(Calendar.DAY_OF_MONTH));
			
			setDate2();
		}
	}
	
	private void setToDate()
	{
		Calendar cal = Calendar.getInstance();
		
		year1 = cal.get(Calendar.YEAR);
		month1 = cal.get(Calendar.MONTH);
		day1 = cal.get(Calendar.DAY_OF_MONTH);
		
		year2 = cal.get(Calendar.YEAR);
		month2 = cal.get(Calendar.MONTH);
		day2 = cal.get(Calendar.DAY_OF_MONTH);
		
		setDate1();
		setDate2();
	}
	
	private void setOneWeekDate()
	{
		Calendar cal = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		
		cal.add(Calendar.DAY_OF_MONTH, -6);
		year1 = cal.get(Calendar.YEAR);
		month1 = cal.get(Calendar.MONTH);
		day1 = cal.get(Calendar.DAY_OF_MONTH);
		
		year2 = cal2.get(Calendar.YEAR);
		month2 = cal2.get(Calendar.MONTH);
		day2 = cal2.get(Calendar.DAY_OF_MONTH);
		
		setDate1();
		setDate2();
	}
	
	private void setOneMonthDate()
	{
		Calendar cal = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		
		cal.add(Calendar.MONTH, -1);
		cal.add(Calendar.DAY_OF_MONTH, 1);
		year1 = cal.get(Calendar.YEAR);
		month1 = cal.get(Calendar.MONTH);
		day1 = cal.get(Calendar.DAY_OF_MONTH);
		
		year2 = cal2.get(Calendar.YEAR);
		month2 = cal2.get(Calendar.MONTH);
		day2 = cal2.get(Calendar.DAY_OF_MONTH);
		
		setDate1();
		setDate2();
	}
	
	private void setThreeMonthDate()
	{
		Calendar cal = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		
		cal.add(Calendar.MONTH, -3);
		cal.add(Calendar.DAY_OF_MONTH, 1);
		year1 = cal.get(Calendar.YEAR);
		month1 = cal.get(Calendar.MONTH);
		day1 = cal.get(Calendar.DAY_OF_MONTH);
		
		year2 = cal2.get(Calendar.YEAR);
		month2 = cal2.get(Calendar.MONTH);
		day2 = cal2.get(Calendar.DAY_OF_MONTH);
		
		setDate1();
		setDate2();
	}
	
	private void setHalfYearDate()
	{
		Calendar cal = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		
		cal.add(Calendar.MONTH, -6);
		cal.add(Calendar.DAY_OF_MONTH, 1);
		year1 = cal.get(Calendar.YEAR);
		month1 = cal.get(Calendar.MONTH);
		day1 = cal.get(Calendar.DAY_OF_MONTH);
		
		year2 = cal2.get(Calendar.YEAR);
		month2 = cal2.get(Calendar.MONTH);
		day2 = cal2.get(Calendar.DAY_OF_MONTH);
		
		setDate1();
		setDate2();
	}
	
	private void setOneYearDate()
	{
		Calendar cal = Calendar.getInstance();
		Calendar cal2 = Calendar.getInstance();
		
		cal.add(Calendar.YEAR, -1);
		cal.add(Calendar.DAY_OF_MONTH, 1);
		year1 = cal.get(Calendar.YEAR);
		month1 = cal.get(Calendar.MONTH);
		day1 = cal.get(Calendar.DAY_OF_MONTH);
		
		year2 = cal2.get(Calendar.YEAR);
		month2 = cal2.get(Calendar.MONTH);
		day2 = cal2.get(Calendar.DAY_OF_MONTH);
		
		setDate1();
		setDate2();
	}
	
	private void setDate1()
	{
		date1TextView.setText(MilkUtil.getDate(year1, month1+1, day1));
	}
	
	private void setDate2()
	{
		date2TextView.setText(MilkUtil.getDate(year2, month2+1, day2));
	}
	
	private boolean setDateCheck(int date1, int date2)
	{
		if (date1 > date2)
		{
			MilkUtil.showToast(superContext, "앞에 날짜가 뒤에 날짜보다 클수 없습니다.");
			return false;
		}
		
		return true;
	}

	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(superContext, TipInfoActivity.class);
		intent.putExtra(TipInfoActivity.TIP_ID, TipMap.TIP_AUTO_PAST_SMS);
		startActivity(intent);
	}

}
